package TextRank;

import org.lionsoul.jcseg.sentence.Sentence;
import org.lionsoul.jcseg.sentence.SentenceSeg;
import org.lionsoul.jcseg.util.StringUtil;

import java.io.IOException;
import java.io.Reader;

public class SelfSeg extends SentenceSeg {
    public SelfSeg(Reader reader) throws IOException {
        super(reader);
    }

    //重写方法，使得以换行符为分隔符
    //get the next sentence
    @Override
    public Sentence next() throws IOException {
        gisb.clear();
        int c, pos = -1;
        while ( (c = readNext()) != -1 ) {
            //clear the whitespace of the begainning
            if ( StringUtil.isWhitespace(c) ) continue;
            if ( c == '\n' || c == '\t' || c == '…' ) continue;
            if ( StringUtil.isCnPunctuation(c) ) {
                switch ( (char)c ) {
                    case '“':
                    case '【':
                    case '（':
                    case '《':
//                    case '｛':
                        break;
                    default: continue;
                }
            }

            if ( StringUtil.isEnPunctuation(c)) {
                switch ( (char)c ) {
                    case '"':
                /*case '[':
                case '(':
                case '{':
                case '<':*/
                        break;
                    default: continue;
                }
            }

            pos = idx;
            gisb.clear().append((char)c);

            while ( (c = readNext()) != -1 ) {
                boolean endTag = false;

                /*
                 * here, define the sentence end tag
                 * punctuation like the following:
                 * .。\n;；?？!！:：
                 */
                switch ((char)c) {
                    case '"': gisb.append('"'); readUntil('"');  break;
                    case '“': gisb.append('“'); readUntil('”');  break;
                    case '【': gisb.append('【'); readUntil('】'); break;
                    case '《': gisb.append('《'); readUntil('》'); break;
//                    case '.': {
//                        int chr = readNext();
//                        gisb.append((char)c);
//                        if ( StringUtil.isEnLetter(chr) )  {
//                            reader.unread(chr);
//                            continue;
//                        } else {
//                            endTag = true;
//                        }
//
//                        break;
//                    }
//                    case '。':
//                    case ';':
//                    case '；':
//                    case '?':
//                    case '？':
//                    case '!':
//                    case '！':
//                    case '…': {
//                        endTag = true;
//                        gisb.append((char)c);
//                        break;
//                    }
//                    case ':':
//                    case '：':
                    case '\n': {
                        endTag = true;
                        break;
                    }
                    default:
                        gisb.append((char)c);
                }

                if ( endTag ) break;
            }

            //clear the whitespace from the back
            for ( int i = gisb.length() - 1; i >= 0; i-- ) {
                char chr = gisb.charAt(i);
                if ( chr == ' ' || chr == '\t' ) gisb.deleteCharAt(i);
                else break;
            }

            if ( gisb.length() <= 1 ) continue;
            return new Sentence(gisb.toString(), pos);
        }
        return null;
    }

    public SelfSeg() throws IOException {
    }
}
